<template>
    <div class="major-add">
        <el-form label-width="80px" ref="form" :model="form" :rules="rules">
            <el-form-item label="用户名" prop="username">
                <el-input v-model="form.username" placeholder="请输入用户名" />
            </el-form-item>
            <el-form-item label="权限" prop="privilege">
                <el-checkbox-group v-model="form.privilege">
                    <el-checkbox v-for="item in privilegeOptions" :label="item.value">{{ item.label }}</el-checkbox>
                </el-checkbox-group>
            </el-form-item>
            <el-form-item label="密码" prop="password">
                <el-input v-model="form.password" placeholder="请输入密码(六位)" />
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer" style="display: flex; justify-content: flex-end;">
            <el-button type="primary" @click="onsubmit">确定</el-button>
            <el-button type="primary" @click="handclose" plain>取消</el-button>
        </span>
    </div>
</template>
<script>
import { adminadminid, adminadminup } from '@/api/admin';

export default {
    props: {
        id: { type: Number, default: null }
    },
    data() {
        return {
            form: { username: '', id: "", privilege: [], password: '' },
            privilegeOptions: [
                { label: '系管理', value: 1 },
                { label: '专业管理', value: 2 },
                { label: '班级管理', value: 4 },
                { label: '学生管理', value: 8 },
                { label: '教师管理', value: 16 },
                { label: '课程管理', value: 32 },
                { label: '选课修改', value: 64 },
                { label: '管理员管理', value: 128 }
            ],
            rules: {
                username: [
                    { required: true, message: '请输入用户名', trigger: 'blur' }
                ],
                password: [
                    { required: true, message: '请输入密码', trigger: 'blur' }
                ],
            },
        }
    },
    mounted() {
        if (this.id) {
            adminadminid(this.id).then(res => {
                if (res.code === 0) {
                    console.log(res);
                    this.form = res.data;
                     if (typeof this.form.privilege === 'number') {
                    const arr = [];
                    this.privilegeOptions.forEach(opt => {
                        if ((this.form.privilege & opt.value) === opt.value) {
                            arr.push(opt.value);
                        }
                    });
                    this.form.privilege = arr;
                }
                } else {
                    this.$message.error('获取专业信息失败');
                }
            });
        }
    },
    methods: {
        async onsubmit() {
            this.$refs["form"].validate(async flag => {
                if (flag) {
                    this.form.privilege = this.form.privilege.reduce((total, num) => total + num, 0);
                    console.log(this.form)
                    const res = await adminadminup(this.form);
                    if (res.code == 0) {
                        this.$message({
                            message: '操作成功',
                            type: 'success',
                        });
                        this.$emit('done');
                    } else {
                        console.log(res);
                        this.$message({
                            message: '操作失败',
                            type: 'error',
                        });
                    }
                }
            })
        },
        handclose() {
            this.$emit('close')
        }
    }
}
</script>
